package com.spzx.order.controller;

import java.util.List;
import java.util.Arrays;

import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.spzx.common.security.annotation.RequiresLogin;
import com.spzx.order.domain.vo.OrderTradeVO;
import com.spzx.user.api.domain.UserInfo;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.spzx.common.log.annotation.Log;
import com.spzx.common.log.enums.BusinessType;
import com.spzx.common.security.annotation.RequiresPermissions;
import com.spzx.order.domain.OrderInfo;
import com.spzx.order.service.IOrderInfoService;
import com.spzx.common.core.web.controller.BaseController;
import com.spzx.common.core.web.domain.AjaxResult;
import com.spzx.common.core.utils.poi.ExcelUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import com.spzx.common.core.web.page.TableDataInfo;

/**
 * 订单Controller
 *
 * @author atguigu
 * @date 2024-07-03
 */
@Tag(name = "订单接口管理")
@RestController
@RequestMapping("/orderInfo/")
public class OrderInfoController extends BaseController {
    @Autowired
    private IOrderInfoService orderInfoService;


    // http://localhost:8080/order/orderInfo/trade
    // trade: 去结算接口
    //  查询当前用户购物车中选中的购物项转为页面需要的数据OrderItem  展示
    // 远程访问购物车查询选中的购物项


    @Operation(summary = "获取订单信息")
    @RequiresLogin
    @GetMapping("getOrderInfo/{orderId}")
    public AjaxResult getOrderInfo(@PathVariable Long orderId) {
        OrderInfo orderInfo = orderInfoService.selectOrderInfoById(orderId);
        return success(orderInfo);
    }

    @Operation(summary = "查询用户的订单列表")
    @GetMapping("userOrderInfoList/{pageNum}/{pageSize}")
    public TableDataInfo userOrderInfoList(@PathVariable("pageNum") Integer pageNum,
                                           @PathVariable("pageSize") Integer pageSize,
                                           @RequestParam(value = "orderStatus", required = false)
                                           Integer orderStatus) {
        return getDataTable(orderInfoService.userOrderInfoList(pageNum, pageSize, orderStatus));
    }


    @Operation(summary = "结算页面的查看订单详情")
    @GetMapping("getOrderInfo/{orderNo}")
    public AjaxResult getOrderInfo(@PathVariable("orderNo") String orderNo) {
        return success(orderInfoService.getOne(Wrappers.lambdaQuery(OrderInfo.class)
                .eq(OrderInfo::getOrderNo, orderNo)));

    }

    @Operation(summary = "结算接口:不会持久化订单数据")
    @GetMapping("trade")
    public AjaxResult trade() {
        OrderTradeVO orderTradeVO = orderInfoService.trade();
        return success(orderTradeVO);
    }


    @Operation(summary = "结算接口:不会持久化订单数据")
    @PostMapping("/submitOrder/")
    public AjaxResult submitOrder(@RequestBody OrderInfo orderInfo) {
        String orderNo = orderInfoService.submitOrder(orderInfo);
        return AjaxResult.success("创建成功", orderNo);
    }


//
//    @Operation(summary = "查询订单列表")
//    @RequiresPermissions("user:orderInfo:list")
//    @GetMapping("/list")
//    public  TableDataInfo selectList(OrderInfo orderInfo){
//       return getDataTable( orderInfoService.selectList(orderInfo));
//    }

//    /**
//     * 查询订单列表
//     */
//    @Operation(summary = "查询订单列表")
//    @RequiresPermissions("order:orderInfo:list")
//    @GetMapping("/list")
//    public TableDataInfo list(OrderInfo orderInfo)
//    {
//        startPage();
//        List<OrderInfo> list = orderInfoService.selectOrderInfoList(orderInfo);
//        return getDataTable(list);
//    }
//
//    /**
//     * 导出订单列表
//     */
//    @Operation(summary = "导出订单列表")
//    @RequiresPermissions("order:orderInfo:export")
//    @Log(title = "订单", businessType = BusinessType.EXPORT)
//    @PostMapping("/export")
//    public void export(HttpServletResponse response, OrderInfo orderInfo)
//    {
//        List<OrderInfo> list = orderInfoService.selectOrderInfoList(orderInfo);
//        ExcelUtil<OrderInfo> util = new ExcelUtil<OrderInfo>(OrderInfo.class);
//        util.exportExcel(response, list, "订单数据");
//    }
//
//    /**
//     * 获取订单详细信息
//     */
//    @Operation(summary = "获取订单详细信息")
//    @RequiresPermissions("order:orderInfo:query")
//    @GetMapping(value = "/{id}")
//    public AjaxResult getInfo(@PathVariable("id") Long id)
//    {
//        return success(orderInfoService.getById(id));
//    }
//
//    /**
//     * 新增订单
//     */
//    @Operation(summary = "新增订单")
//    @RequiresPermissions("order:orderInfo:add")
//    @Log(title = "订单", businessType = BusinessType.INSERT)
//    @PostMapping
//    public AjaxResult add(@RequestBody OrderInfo orderInfo)
//    {
//        return toAjax(orderInfoService.save(orderInfo));
//    }
//
//    /**
//     * 修改订单
//     */
//    @Operation(summary = "修改订单")
//    @RequiresPermissions("order:orderInfo:edit")
//    @Log(title = "订单", businessType = BusinessType.UPDATE)
//    @PutMapping
//    public AjaxResult edit(@RequestBody OrderInfo orderInfo)
//    {
//        return toAjax(orderInfoService.updateById(orderInfo));
//    }
//
//    /**
//     * 删除订单
//     */
//    @Operation(summary = "删除订单")
//    @RequiresPermissions("order:orderInfo:remove")
//    @Log(title = "订单", businessType = BusinessType.DELETE)
//	@DeleteMapping("/{ids}")
//    public AjaxResult remove(@PathVariable Long[] ids)
//    {
//        return toAjax(orderInfoService.removeBatchByIds(Arrays.asList(ids)));
//    }
}
